package com.ddzj.scaffold.controller;

import com.ddzj.scaffold.dto.converterentity.UserConverter;
import com.ddzj.scaffold.entity.User;
import com.ddzj.scaffold.enums.AjaxResultStatusEnum;
import com.ddzj.scaffold.service.UserService;
import com.ddzj.scaffold.vo.AjaxResultVo;
import com.ddzj.scaffold.dto.UserRegisterDto;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户注册控制层
 * @Author:yangzibo
 * @CreateTime:2024-04-11 12:57
 * @Description:
 * @Version:1.0
 **/
@RestController
@RequestMapping("/userRegister")
public class UserRegisterController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserConverter userConverter;

    /**
     * 根据用户账号查询用户是否存在
     * @param account
     * @return
     */
    @GetMapping("/findExistsUserByAccount/{account}")
    public AjaxResultVo findExistsUserByAccount(@PathVariable("account") String account){
        Long num = userService.findExistsUserByAccount(account);
        if(num == 0){
            return AjaxResultVo.ok();
        }else{
            return AjaxResultVo.error(AjaxResultStatusEnum.S_ERROR, "账号已存在！");
        }
    }

    @PostMapping("/save")
    public AjaxResultVo save(@Valid @RequestBody UserRegisterDto reqVo){
        User user = userConverter.userRegisterDtoToUser(reqVo);
        Long num = userService.findExistsUserByAccount(reqVo.getAccount());
        if(num > 0){
            return AjaxResultVo.error(AjaxResultStatusEnum.S_ERROR, "账号已存在！");
        }
        userService.saveRegisterUserInfo(user);
        return AjaxResultVo.ok();
    }
}
